﻿var orders, customers, details;

function pageLoad() {
    orders = $create(Sys.UI.DataView, { selectedItemClass: "selectedRow" }, {}, {}, $get("orders"));
    orders.add_command(OrderRowSelected);
    customers = $create(Sys.UI.DataView, { selectedItemClass: "selectedRow" }, {}, {}, $get("customers"));
    customers.add_command(CustomerRowSelected);
    details = $create(Sys.UI.DataView, { selectedItemClass: "selectedRow" }, {}, {}, $get("details"));

    ScottCateAjax2008.Services.NorthwindService.GetCustomers(GetCustomers_Callback, errorCallback);
}

function GetCustomers_Callback(results) {
    customers.set_data(results);
}

function CustomerRowSelected(sender, args) {
    var customerId = args.get_commandSource().innerText;
    ScottCateAjax2008.Services.NorthwindService.GetCustomerOrders(customerId, GetCustomerOrders_Callback, errorCallback);
}

function GetCustomerOrders_Callback(results) {
    orders.set_data(results);
}

function OrderRowSelected(sender, args) {
    var orderId = args.get_commandSource().innerText;
    ScottCateAjax2008.Services.NorthwindService.GetOrderDetails(orderId, GetOrderDetails_Callback, errorCallback);
}

function GetOrderDetails_Callback(results) {
    details.set_data(results);
}

function errorCallback(error) {
    alert(error);
}